package com.api.edc.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.api.edc.model.ApiConstant;
import com.api.edc.model.MapResult;
import com.nova.edc.model.system.Login;
import com.nova.edc.model.system.ModelConstant;
import com.nova.edc.service.energy.EnergyService;
import com.nova.frame.utils.StringUtils;
import com.seio.edc.util.StringUtil;

@Controller
@RequestMapping(value = "/appStatistics")
public class StatisticsAPIController {

	@Autowired
	private EnergyService novaEnergyService;

	// 综合经营分析表-----------获取某段时间每个单位的单位生产值、单位人员耗电量、单位面积耗电量
	@RequestMapping(value = "getUnitEnergyByDate.shtml")
	@ResponseBody
	public MapResult getUnitEnergyByDate(HttpServletRequest request,
			HttpSession session, String companyCode, String startDate,
			String endDate) {
		MapResult mapResult = new MapResult();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			if (StringUtil.isEmpty(startDate)) {
				startDate = ModelConstant.MIN_MONTH_DATE;
			}

			if (StringUtil.isEmpty(endDate)) {
				endDate = ModelConstant.MAX_MONTH_DATE;
			}
			if (StringUtils.isEmpty(companyCode)) {
				companyCode = session.getAttribute("companycode").toString();
			}
			list = novaEnergyService.getUnitEnergyByDate(companyCode,
					startDate, endDate);
			mapResult.setData(list);
			mapResult.setCode(ApiConstant.CODE_SUCCESS);
			mapResult.setMessage(ApiConstant.MESSAGE_SUCCESS);
		} catch (Exception e) {
			mapResult.setCode(ApiConstant.CODE_EXCEPTION);
			mapResult.setMessage(ApiConstant.MESSAGE_EXCEPTION);
		}

		return mapResult;
	}

	// 获取某段时间每个单位通宵供电的次数
	@RequestMapping(value = "getAllNightByDate.shtml")
	@ResponseBody
	public MapResult getAllNightByDate(HttpServletRequest request,
			HttpSession session, String companyCode, String startDate,
			String endDate) {
		MapResult mapResult = new MapResult();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			if (StringUtil.isEmpty(startDate)) {
				startDate = ModelConstant.MIN_MONTH_DATE;
			}

			if (StringUtil.isEmpty(endDate)) {
				endDate = ModelConstant.MAX_MONTH_DATE;
			}
			if (StringUtils.isEmpty(companyCode)) {
				companyCode = session.getAttribute("companycode").toString();
			}
			list = novaEnergyService.getAllNightByDate(companyCode,
					ModelConstant.MIN_TIME, ModelConstant.MAX_TIME, startDate,
					endDate);

			mapResult.setData(list);
			mapResult.setCode(ApiConstant.CODE_SUCCESS);
			mapResult.setMessage(ApiConstant.MESSAGE_SUCCESS);
		} catch (Exception e) {
			mapResult.setCode(ApiConstant.CODE_EXCEPTION);
			mapResult.setMessage(ApiConstant.MESSAGE_EXCEPTION);
		}

		return mapResult;
	}

	// 用电分析表-----------获取某个单位每天的耗电量
	@RequestMapping(value = "findPowerEveryDay.shtml")
	@ResponseBody
	public MapResult findPowerEveryDay(HttpServletRequest request,
			HttpSession session, Integer companyCode, String startDate,
			String endDate) {
		MapResult mapResult = new MapResult();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			if (StringUtil.isEmpty(startDate)) {
				startDate = ModelConstant.MIN_DATE;
			}

			if (StringUtil.isEmpty(endDate)) {
				endDate = ModelConstant.MAX_DATE;
			}
			if (companyCode == null || companyCode == 0) {
				Login login = (Login) session
						.getAttribute(ApiConstant.APP_SESSION_LOGIN);
				companyCode = Integer.parseInt(login.getCompanycode());
			}
			list = novaEnergyService.getEnergyByDate(companyCode, startDate,
					endDate);
			mapResult.setData(list);
			mapResult.setCode(ApiConstant.CODE_SUCCESS);
			mapResult.setMessage(ApiConstant.MESSAGE_SUCCESS);
		} catch (Exception e) {
			mapResult.setCode(ApiConstant.CODE_EXCEPTION);
			mapResult.setMessage(ApiConstant.MESSAGE_EXCEPTION);
		}

		return mapResult;
	}

	// 节电分析表----------获取某个单位每天的LED耗电量和传统灯具耗电量
	@RequestMapping(value = "getAllEnergyByDate.shtml")
	@ResponseBody
	public MapResult getAllEnergyByDate(HttpServletRequest request,
			HttpSession session, Integer companyCode, String startDate,
			String endDate) {
		MapResult mapResult = new MapResult();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			if (StringUtil.isEmpty(startDate)) {
				startDate = ModelConstant.MIN_DATE;
			}

			if (StringUtil.isEmpty(endDate)) {
				endDate = ModelConstant.MAX_DATE;
			}
			if (companyCode == null || companyCode == 0) {
				companyCode = (Integer) session.getAttribute("companycode");
			}
			list = novaEnergyService.getAllEnergyByDate(companyCode, startDate,
					endDate);
			mapResult.setData(list);
			mapResult.setCode(ApiConstant.CODE_SUCCESS);
			mapResult.setMessage(ApiConstant.MESSAGE_SUCCESS);
		} catch (Exception e) {
			mapResult.setCode(ApiConstant.CODE_EXCEPTION);
			mapResult.setMessage(ApiConstant.MESSAGE_EXCEPTION);
		}

		return mapResult;
	}

	// 区域用电分析表--------获取某个单位一段时间内每个区域的耗电量
	@RequestMapping(value = "findPowerEveryRegion.shtml")
	@ResponseBody
	public MapResult findPowerEveryRegion(HttpServletRequest request,
			HttpSession session, Integer companyCode, String startDate,
			String endDate) {
		MapResult mapResult = new MapResult();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			if (StringUtil.isEmpty(startDate)) {
				startDate = ModelConstant.MIN_DATE;
			}

			if (StringUtil.isEmpty(endDate)) {
				endDate = ModelConstant.MAX_DATE;
			}
			if (companyCode == null || companyCode == 0) {
				companyCode = (Integer) session.getAttribute("companycode");
			}
			list = novaEnergyService.getEnergyByRegion(companyCode, startDate,
					endDate);
			mapResult.setData(list);
			mapResult.setCode(ApiConstant.CODE_SUCCESS);
			mapResult.setMessage(ApiConstant.MESSAGE_SUCCESS);
		} catch (Exception e) {
			mapResult.setCode(ApiConstant.CODE_EXCEPTION);
			mapResult.setMessage(ApiConstant.MESSAGE_EXCEPTION);
		}

		return mapResult;
	}
}
